<?php
global $db;
require_once "../../tool/sql.php";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
session_start();
$data = $_POST["list_name"];
if (!isset($_POST["cache_word_ids"])) {
    if (strlen($data) < 1) {
        die("Data Error");
    }
    $res = $db->selectById("userinfo", "public_cache", "name", $_SESSION['user']);
    if ($res == null)
        die($db->getStmtError());
    $public_cache = "";
    foreach ($res as $r) {
        $public_cache = $r['public_cache'];
    }
    if (strlen($public_cache) < 2) {
        $public_cache = json_encode([]);
    }
    $wordlist = "";
    $res = $db->selectById("publiclist", "text", "name", $data);
    if ($res == null)
        die($db->getStmtError());
    foreach ($res as $r) {
        $wordlist = $r['text'];
    }
    echo $public_cache."\n".$wordlist;
} else {
    if(!$db->update("userinfo","name='".$_SESSION['user']."'",["public_cache"=>$_POST["cache_word_ids"]])) {
        echo $db->getStmtError();
    } else {
        echo "0";
    }
}